package com.example.eos.mapper;

import com.example.eos.bean.Publish;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

@Mapper
@CacheConfig(cacheNames = "public")
public interface PublishMapper {

    //查询发表列表
    @Select("select publishid, userid, labelnames, introduce, picadd1, picadd2, picadd3, picadd4, picadd5, picadd6, picadd7, picadd8, picadd9, location, likenum, commentnum, publishtime from publish_view where labelname = #{labelname,jdbcType=VARCHAR} order by publishtime desc")
    List<Publish> findPublishByLabelname(String labelname);

    //查询发表列表
    @Select("select publishid, userid, labelnames, introduce, picadd1, picadd2, picadd3, picadd4, picadd5, picadd6, picadd7, picadd8, picadd9, location, likenum, commentnum, publishtime from publish order by likenum desc")
    List<Publish> findPublishs();

    //查询发表（发表id）
    @Select("select publishid, userid, labelnames, introduce, picadd1, picadd2, picadd3, picadd4, picadd5, picadd6, picadd7, picadd8, picadd9, location, likenum, commentnum, publishtime from publish where publishid = #{publishid,jdbcType=VARCHAR}")
    @Cacheable(key ="#p0")
    Publish findByPublishid(String publishid);

    //新建发表
    @Insert("insert into publish (publishid, userid, labelnames, introduce, picadd1, picadd2, picadd3, picadd4, picadd5, picadd6, picadd7, picadd8, picadd9, location, likenum, commentnum, publishtime) values (#{publishid,jdbcType=VARCHAR}, #{userid,jdbcType=VARCHAR}," +
            " #{labelnames,jdbcType=VARCHAR}, #{introduce,jdbcType=VARCHAR}, #{picadd1,jdbcType=VARCHAR}, #{picadd2,jdbcType=VARCHAR}, #{picadd3,jdbcType=VARCHAR}, #{picadd4,jdbcType=VARCHAR}, #{picadd5,jdbcType=VARCHAR}, #{picadd6,jdbcType=VARCHAR}," +
            " #{picadd7,jdbcType=VARCHAR}, #{picadd8,jdbcType=VARCHAR}, #{picadd9,jdbcType=VARCHAR}, #{location,jdbcType=VARCHAR}, #{likenum,jdbcType=INTEGER}, #{commentnum,jdbcType=INTEGER}, #{publishtime})")
    void save(Publish publish);

    //更新发表
    @Update("update publish set labelnames = #{labelnames,jdbcType=VARCHAR}, introduce = #{introduce,jdbcType=VARCHAR}, picadd1 = #{picadd1,jdbcType=VARCHAR}, picadd2 = #{picadd2,jdbcType=VARCHAR}, picadd3 = #{picadd3,jdbcType=VARCHAR}, picadd4 = #{picadd4,jdbcType=VARCHAR}," +
            " picadd5 = #{picadd5,jdbcType=VARCHAR}, picadd6 = #{picadd6,jdbcType=VARCHAR}, picadd7 = #{picadd7,jdbcType=VARCHAR}, picadd8 = #{picadd8,jdbcType=VARCHAR}, picadd9 = #{picadd9,jdbcType=VARCHAR}, location = #{location,jdbcType=VARCHAR}" +
            " where publishid = #{publishid,jdbcType=VARCHAR}")
    void update(Publish publish);

    //删除发表
    @Delete("delete from publish where publishid = #{publishid,jdbcType=VARCHAR}")
    void deleteByPublishid(String publishid);

    //发表点赞
    @Update("update publish set likenum = likenum+1 where publishid = #{publishid,jdbcType=VARCHAR}")
    void updateLikenum(String publishid);

    //发表评论
    @Update("update publish set commentnum = commentnum+1 where publishid = #{publishid,jdbcType=VARCHAR}")
    void updateCommentnum(String publishid);

}
